#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#define STRLEN_MAX         20
#define PASS_MAX       200000
#define CASHDESK_MAX   100000
#define NMAX         PASS_MAX

struct Customer {
	int num;
	struct Customer* next;
};

/*
 * { X, next }  { Y, next }  { Z, next }
 *     ^ \      ^       \    ^   ^  \
 *     |  `-----'        `---'   |   `-- NULL
 *    head                      tail
 *
 * next HEAD[i] TAIL[i] are of the same type
 */

/* {{{ CTAKAH */
#ifndef STACK_H
#define STACK_H

#ifndef STACK_DATA_TYPE
#define STACK_DATA_TYPE int
#endif /* #ifndef STACK_DATA_TYPE */
struct Stack {
	STACK_DATA_TYPE data[PASS_MAX+2];
	int size;
};
#endif /* #ifndef STACK_H */


extern void
Clear(struct Stack * s)
{
	s->size = 0;
}



extern int
Push(struct Stack * s, STACK_DATA_TYPE new_)
{
	if (s->size == PASS_MAX)
		return -1;
	s->data[s->size] = new_;
	++s->size;
	return 0;
}



extern STACK_DATA_TYPE
Pop(struct Stack * s)
{
	if (s->size)
		--s->size;
	return s->data[s->size];
}



extern STACK_DATA_TYPE
Top(const struct Stack * s)
{
	return s->data[s->size - 1];
}



extern unsigned char
IsEmpty(const struct Stack * s)
{
	return s->size ? 0 : 1;
}
/* }}} */


int
main(int argc, char ** argv)
{
	static struct Customer station[PASS_MAX+2];
	static struct Customer *head[CASHDESK_MAX+1];
	static struct Customer *tail[CASHDESK_MAX+1];
	struct Customer *ptr;
	static struct Stack free_cells;
	int k, queue_n;
	register int i = 1; /* passenger number */
	char line[STRLEN_MAX+2], *line_p;
	register int m;

	(void) freopen("station.in", "r", stdin);
	(void) freopen("station.out", "w", stdout);

	(void) scanf("%d\n", &k);

	/*
	 * initialization
	 */
	for (m=1; m <= k ; ++m) {
		head[m]         = &station[m];
		tail[m]         = head[m];
		station[m].next = NULL;
	}
	Clear(&free_cells);
	m = PASS_MAX+2;
	while (--m > k)
		Push(&free_cells, m);

	/*
	 * 1.  get the queue number from the string.
	 * 2a. if we're adding a passenger to a queue, make
	 * 	 tail[N]->next refer to a valid cell and fill in
	 * 	 the passenger's data.
	 * 2b. or else, (we've served a passenger) print their info,
	 *	 release their cell and switch to a next passenger
	 *	 (after him).
	 */
	while (fgets(line, STRLEN_MAX, stdin)) {
		queue_n = (int) strtol(line, &line_p, 10);

		if (queue_n == 0)
			return 0;

		if (strstr(line_p, "queued up")) {
			if (head[queue_n] == tail[queue_n]) {
				/*
				 * Initialize empty queue
				 */
				tail[queue_n]->next = &station[Pop(&free_cells)];
				tail[queue_n]       = tail[queue_n]->next;
			}

			/*
			 * Fill data of next passenger
			 */
			tail[queue_n]->next   = f;
			tail[queue_n]->num    = i++;
		} else {
			printf("%d\n", head[queue_n]->num);
			/* Add cell's index to free_cells stack */
			if (Push(&free_cells, head[queue_n] - &station[0]))
				fprintf(stderr, "%s: failed to push: stack overflow :D\n", argv[0]);

			/* switch to the next cell "after" current one */
			if (head[queue_n]->next)
				head[queue_n] = head[queue_n]->next;
			else {
				head[queue_n]         = &station[Pop(&free_cells)];
				head[queue_n]->next   = NULL;
				tail[queue_n]         = head[queue_n];
			}
		}

		/* print_queue (inlined) */
		for (m = 1 ; m <= k ; ++m) {
			ptr = head[m];
			while (ptr != NULL) {
				(void) printf("queue_n=%5d cell: { num=%6d, next=%8p } (%p)\n", m, ptr->num, (void*)ptr->next, (void*)ptr);
				ptr = ptr->next;
			}
		}
		putc('\n', stdout);
	}

	return 0;
}
// vim:foldmethod=marker:
